package com.csdig.cms.service.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.csdig.cms.common.RealPathResolver;
import com.csdig.cms.model.Channel;
import com.csdig.cms.model.Content;
import com.csdig.cms.service.ChannelService;
import com.csdig.cms.service.ContentService;
import com.csdig.cms.service.FrontPageService;
import com.csdig.cms.utils.FrontUtils;

@Service
public class FrontPageServiceImpl implements FrontPageService {

	@Autowired
	private ChannelService channelService;

	@Autowired
	private ContentService contentService;

	@Autowired
	private RealPathResolver realPathResolver;

	@Resource(name = "freemarkerConfig")
	private FreeMarkerConfigurer conf;

	@Override
	public String channel(String path, int pageNo, String[] params, Map<String, Object> model) throws Exception {
		Channel channel = channelService.findByPath(path);
		if (channel != null) {
			model.put("channel", channel);
			//单 页
			if (channel.getIsSingle()) {
				Content content = contentService.findByPath(path);
				model.put("content", content);
			}
			return channel.getTplPath();
		}
		return FrontUtils.pageNotFound(model);
	}

	@Override
	public String content(String path, int pageNo, String[] params, Map<String, Object> model) throws Exception {
		Content content = contentService.findByPath(path);
		if (content != null) {
			Integer channelId = content.getChannelId();
			Channel channel = channelService.getById(channelId);

			model.put("content", content);
			model.put("channel", channel);
			return channel.getContentPath();
		}
		return FrontUtils.pageNotFound(model);
	}

}
